﻿Imports System.Collections.Generic
Imports System.Linq

''' <summary>
''' Represents a set of nodes that can be dependent upon each other, 
''' and will detect circular dependencies between its nodes.
''' </summary>
Public Class Graph

#Region "Constructor"

    Public Sub New(ByVal title As String)
        'Me.CircularDependencies = new List<CircularDependency>()
        Me.Nodes = New List(Of Node)
        Me.Title = title
    End Sub

#End Region ' Constructor

#Region "Properties"

    'public List<CircularDependency> CircularDependencies { get; private set; }

    'public bool HasCircularDependency
    '{
    '   get { return this.CircularDependencies.Any(); }
    '}
    Private _Nodes As List(Of Node)
    Public Property Nodes As List(Of Node)
        Get
            Return _Nodes
        End Get
        Private Set(ByVal value As List(Of Node))
            _Nodes = value
        End Set
    End Property

    Private _Title As String
    Public Property Title As String
        Get
            Return _Title
        End Get
        Private Set(ByVal value As String)
            _Title = value
        End Set
    End Property

#End Region ' Properties

#Region "Methods"

    ' ''' <summary>
    ' ''' Inspects the graph's nodes for circular dependencies.
    ' ''' </summary>
    'Public Sub CheckForCircularDependencies()
    '    For Each node As Node In Me.Nodes
    '        var(circularDependencies = node.FindCircularDependencies())
    '        If (circularDependencies IsNot null) Then Me.ProcessCircularDependencies(circularDependencies)
    '    Next

    '    Me.CircularDependencies.Sort()
    'End Sub

    '    Void(ProcessCircularDependencies(List < CircularDependency > circularDependencies))
    '{
    '    foreach (CircularDependency circularDependency in circularDependencies)
    '    {
    '        if (circularDependency.Nodes.Count == 0)
    '            continue;

    '        If (this.CircularDependencies.Contains(circularDependency)) Then
    '            continue;

    '        // Arrange the nodes into the order in which they were discovered.
    '        circularDependency.Nodes.Reverse();

    '        this.CircularDependencies.Add(circularDependency);

    '        // Inform each node that it is a member of the circular dependency.
    '        foreach (Node dependency in circularDependency.Nodes)
    '            dependency.CircularDependencies.Add(circularDependency);
    '    }
    '}

#End Region ' Methods

End Class